<template>
    <!-- 导航 -->
    <van-nav-bar title="确认订单" left-text left-arrow @click-left="back"></van-nav-bar>

    <!-- 收货地址 -->
    <!-- 有地址 -->
    <van-contact-card v-if="address" type="edit" :tel="address.mobile" :name="address.consignee" @click="onEdit" />

    <!-- 无地址 -->
    <van-contact-card v-else type="add" add-text="收货地址" @click="onAdd" />

    <!-- 购物车记录 -->
    <van-card
        v-for="item in cartlist"
        :key="item"
        :price="item.total"
        :num="item.nums"
        :desc="'单价：￥' + item.price"
        :title="item.product.name"
        :thumb="item.product.cover_cdn"
        >
        <template #tags>
            <p>库存：{{item.product.stock}}</p>
        </template>
    </van-card>

    <!-- 订单备注 -->
    <van-field
        v-model="content"
        rows="2"
        autosize
        label="订单备注"
        type="textarea"
        maxlength="500"
        placeholder="请输入订单备注"
        show-word-limit
    />

    <!-- 确认订单栏 -->
    <van-submit-bar :price="total" button-text="确认下单" @submit="SubmitOrder" />
</template>
<script>
    export default {
        emits: ['toggle'],
        created() {
            this.$emit('toggle', false);

            this.cartids = this.$route.query.cartids ? this.$route.query.cartids : '';

            if(!this.cartids)
            {
                this.$notify({
                message:'请输入下单商品',
                duration:1000,
                onClose: () => {
                    //返回上一个界面
                    // this.$router.go(-1)
                }
                })

                return false
            }

            //获取购物车数据
            this.CartData()

            let address= this.$cookies.get('address')

            if(address){
                this.address = address
            } else{
                //获取默认收货地址
                this.AddressData()
            }
            
        },
        data(){
            return{ 
                tel: null,
                name: null,
                cartlist: [],
                total: 0,
                content: null,
                cartids: null,
                LoginUser: this.$cookies.get('LoginUser'),
                address: null
            }
        },
        computed:{
            // 合计总数
            total() {
                
                var total =0

                this.cartlist.forEach((item)=>{
                
                    total += parseFloat(item.total)
                    
                })
                total = parseFloat(total) * 100

                return  total
            }
        },
        methods:{
            back(){
                this.$router.push('/product/cart/index')
            },
            onEdit(){
                this.$router.push({
                    path: '/user/address/index',
                    query: {
                        action: 'order'
                    }
                })
            },
            async SubmitOrder() {

                let data = {
                    userid: this.LoginUser.id,
                    addrid: this.address.id,
                    cartids: this.cartids,
                    content: this.content
                }

                let result = await this.$api.OrderAdd(data)

                if (result.code == '1'){
                    this.$notify({
                        type: 'success',
                        message: result.msg,
                        duration: 1000,
                        onClose: () => {
                            this.$router.replace('/user/order/index')
                        }
                    })
                } else {
                    this.$notify({
                        message: result.msg,
                        duration: 1000,
                    })
                    return false
                }

            },
            onAdd(){
                this.$router.push({
                    path: '/user/address/add',
                    query:{
                        action: 'order',
                        cartids: this.cartids
                    }
                })
                // this.$router.replace('home')
            },
            // 获取购物车列表
            async CartData(){
                let data = {
                    userid: this.LoginUser.id,
                    cartids: this.cartids
                }

                let result = await this.$api.AddIndex(data)

                if(result.code == '1'){
                    this.cartlist = result.data
                }else{
                    this.$notify({
                        message:result.msg,
                        duration:1000,
                    })
                    return false
                }
            },
            async AddressData(){

                let result = await this.$api.AddressOrder({userid: this.LoginUser.id})

                if(result.code == '1'){
                    this.address = result.data
                }else{
                    this.$notify({
                        message:result.msg,
                        duration:1000,
                    })
                    return false
                }
            }
        }
    }
</script>